package cn.lagou.Homework.RockPaperScissor

import scala.collection.mutable

class Game {
  val player1:Player = new User("游客")
  var player2:Player = _;
  var resultMap: mutable.Map[String, Int] = mutable.Map("player1w"->0,"player1l"->0,"player1d"->0)

  def initGamePlayer(): Unit = {
    println("*" * 30)
    println("*" * 10 + "猜拳开始" + "*" * 10)
    println("*" * 30)
    println("请选择对战角色（1.刘备 2.关羽 3.张飞）")
    scala.io.StdIn.readInt() % 3 match {
      case 1 => player2= new Computer("刘备")
      case 2 => player2= new Computer("关羽")
      case 0 => player2= new Computer("张飞")
    }
    println(s"你选择了与${player2.name}对战")
  }

  def compareFist(player1fist:String, player2fist:String): Unit ={
    if (player1fist==player2fist) {
      println("结果：和局！下次继续努力")
      player1.score+=1
      player2.score+=1
      resultMap("player1d") += 1
    }
    if (player1fist=="石头" && player2fist=="剪刀" ||
        player1fist=="剪刀" && player2fist=="布" ||
        player1fist=="布" && player2fist=="石头") {
      println("结果：恭喜，你赢啦！")
      player1.score += 2
      resultMap("player1w") += 1
    }
    else {
      println(s"结果：${player2.name}赢了")
      player2.score += 2
      resultMap("player1l") +=1
    }
  }

  def printMatrix(): Unit = {
    println("-"*30)
    println(s"${player2.name} VS ${player1.name}")
    println(s"对战次数${resultMap.values.sum}次")
    print("\n" * 2)
    println(f"${"姓名"}${"得分"}%5s${"胜局"}%5s${"和局"}%5s${"负局"}%5s")
    println(f"${player1.name}${player1.score}%7s${resultMap("player1w")}%7s${resultMap("player1d")}%7s${resultMap("player1l")}%7s")
    println(f"${player2.name}${player2.score}%7s${resultMap("player1l")}%7s${resultMap("player1d")}%7s${resultMap("player1w")}%7s")
    println("-"*30)
  }

  def gameLogic(): Unit = {
    initGamePlayer()
    println("要开始了么？")
    if(scala.io.StdIn.readLine() != "y") return
    do {
      compareFist(player1.showFist(), player2.showFist())
      println("是否开始下一轮（y/n）?")
    } while (scala.io.StdIn.readLine() == "y")
    println("退出游戏！")
    printMatrix()
  }
}

object Game {
  def main(args: Array[String]): Unit = {
    val game = new Game
    game.gameLogic()
  }
}
